package com.fesine.quartz.chapter7;

import com.fesine.quartz.chapter4.PrintJob;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.ParseException;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2016/11/19 20:50
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2016/11/19 20:50
 */
public class Listing_7_3 {
    static Log logger = LogFactory.getLog(Listing_7_3.class);

    public static void main(String[] args) {
        new Listing_7_3().startScheduler();
    }

    private void startScheduler() {
        try {
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

            JobDetail jobDetail = new JobDetail("PrintJob", Scheduler.DEFAULT_GROUP, PrintJob
                    .class);
            CronTrigger trigger = new CronTrigger("myTrigger", Scheduler.DEFAULT_GROUP, "0/10 * *" +
                    " ? * *");
            JobListener listener = new SimpleJobListener();
            //添加全局jobListener
            //scheduler.addGlobalJobListener(listener);
            //添加非全局jobListener,分两步1是scheduler添加jobListener 2是将listener.name添加到jobDetail
            scheduler.addJobListener(listener);
            jobDetail.addJobListener(listener.getName());
            scheduler.scheduleJob(jobDetail, trigger);
            scheduler.start();
            logger.info("scheduler starting up...");
        } catch (SchedulerException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}
